import Vue from 'vue'

Vue.filter('limitWordLength', (str, length, sufix) => {
  /* eslint-disable */
  let r = /[^\x00-\xff]/g // 双字节正则
  let m
  if (str.replace(r, '**').length > length) {
    m = Math.floor(length / 2)
    for (let i = m, l = str.length; i < l; i++) {
      if (str.substr(0, i).replace(r, '**').length >= length) {
        return str.substr(0, i) + sufix
      }
    }
  }
  return str
})
Vue.filter('date', function (value, M) {
  // console.log(value)
  if (M) {
    var date = new Date(value);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return `${y}-${m}-${d} ${h}:${minute}:${second}`
  } else {
    var date = new Date(value);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    return `${y}-${m}-${d}`
  }

})

Vue.filter('userinfo', function (value, M) {
  let array = JSON.parse(localStorage.getItem('userlist'))
  if (array && value) {

    let name = array.filter(item => {
      if (item.id == value) {
        return item
      }
    })
    return name[0] ? name[0].username : ''
  }
})

Vue.filter('companyinfo', function (value, M) {
  let array = JSON.parse(localStorage.getItem('companylist'))
  if (array && value) {

    let name = array.filter(item => {
      if (item.id == value) {
        return item
      }
    })
    return name[0] ? name[0].name : ''
  }
})
Vue.filter('hospitalName', (value, arr) => {
 // console.log(value, arr)
  if (arr && value) {

    let name = arr.filter(item => {
      if (item.id == value) {
        return item
      }
    })
    return name[0] ? name[0].name : ''
  }
})